const Compiler = require('webpack/lib/Compiler');
const Compilation = require('webpack/lib/Compilation');

class A {
    apply(compiler) {
        const {hooks} = compiler;
        for (const key in hooks) {
            const hook = hooks[key];
            hook.tap(key, (err, res) => {
                if (key === 'thisCompilation') {
                    const hooks = [
                        'buildModule',
                        'rebuildModule',
                        'failedModule',
                        'succeedModule',
                        'finishModules',
                        'seal',
                        'unseal',
                        'optimizeDependencies',
                        'afterOptimizeDependencies',
                        'optimize',
                        'optimizeModules',
                        'afterOptimizeModules',
                        'optimizeChunks',
                        'afterOptimizeChunks',
                        'optimizeTree',
                        'afterOptimizeTree',
                        'optimizeChunkModules',
                        'afterOptimizeChunkModules',
                        'shouldRecord',
                        'reviveModules',
                        'beforeModuleIds',
                        'moduleIds',
                        'optimizeModuleIds',
                        'afterOptimizeModuleIds',
                        'reviveChunks',
                        'beforeChunkIds',
                        'chunkIds',
                        'optimizeChunkIds',
                        'afterOptimizeChunkIds',
                        'recordModules',
                        'recordChunks',
                        'beforeModuleHash',
                        'afterModuleHash',
                        'beforeHash',
                        'afterHash',
                        'recordHash',
                        'record',
                        'beforeModuleAssets',
                        'additionalChunkAssets',
                        'shouldGenerateChunkAssets',
                        'beforeChunkAssets',
                        'additionalAssets',
                        'optimizeChunkAssets',
                        'afterOptimizeChunkAssets',
                        'optimizeAssets',
                        'afterOptimizeAssets',
                        'processAssets',
                        'afterProcessAssets',
                        'needAdditionalSeal',
                        'afterSeal',
                        'chunkHash',
                        'moduleAsset',
                        'chunkAsset',
                        'assetPath',
                        'needAdditionalPass',
                        'childCompiler',
                        'normalModuleLoader',
                    ]
                    const compilation = err;
                    hooks.forEach((hookName) => {
                        compilation.hooks[hookName].tap('MyPlugin', (callback) => {
                            console.log('compilationHook', hookName)
                        });
                        // if (hookName === 'additionalAssets') {
                        //     compilation.hooks.additionalAssets.tapAsync('MyPlugin', (callback) => {
                        //         console.log('compilation assets', compilation.assets);
                        //         console.log('compilation modules', compilation.modules);
                        //         console.log('compilation records', compilation.records);
                        //         console.log('compilation assetsInfo', compilation.assetsInfo);
                        //         console.log('compilation fileDependencies', compilation.fileDependencies);
                        //         console.log('compilation moduleGraph', compilation.moduleGraph);
                        //         console.log('compilation entrypoints', compilation.entrypoints);
                        //         console.log('compilation chunks', compilation.chunks);
                        //         console.log('compilation chunkGroups', compilation.chunkGroups);
                        //         callback();
                        //     });
                        // } else {
                        //     err.hooks[hookName].tap(
                        //         'SourceMapDevToolModuleOptionsPlugin',
                        //         (module) => {
                        //             console.log('compilationHook', hookName);
                        //         }
                        //     );
                        // }
                    })
                }

                // if (key === 'thisCompilation') {
                //     for (const thisKey in err.hooks) {
                //         const compilationHook = err.hooks[thisKey];
                //         const hookConstructorName = compilationHook.constructor.name;
                //         const argsLength = (compilationHook._args && compilationHook._args.length) || 0
                //         if (hookConstructorName === 'AsyncSeriesWaterfallHook' ||
                //             hookConstructorName === 'AsyncSeriesLoopHook' ||
                //             hookConstructorName === 'AsyncSeriesHook' ||
                //             hookConstructorName === 'AsyncParallelHook' ||
                //             hookConstructorName === 'AsyncParallelBailHook') {
                //             compilationHook.tap && compilationHook.tap('tabAsync', (...args) => {
                //                 // const next = args.pop();
                //                 console.log('compilationHookName 1', thisKey);
                //                 // next(null);
                //             })
                //         } else {
                //             compilationHook.tap && compilationHook.tap('tap', () => {
                //                 console.log('compilationHookName 2', thisKey)
                //             })
                //         }
                //         console.log('compilationHookName', thisKey, argsLength);
                //     }
                // }
            })
        }
    }

    getParams(argLength, hookName) {
        let res = [hookName];
        const callback = () => {
            console.log('compilationHookName 1', hookName);
        }
        if (argLength === 3) {
            res = [...res, 'test', 'test', callback];
        } else if (argLength === 2) {
            res = [...res, 'test', 'test', callback];
        } else if (argLength === 1) {
            res = [...res, 'test', 'test', callback];
        } else {
            res = [...res, 'test', 'test', callback];
        }
        return res;
    }

    applyCompilation() {

    }
}

module.exports = A;